const fs = require('fs')
const inquirer = require('inquirer')

const download = require('download-git-repo')

const ora = require('ora')

const chalk = require('chalk')

const { frameworkArr, gitAddress } = require('./config.js')
async function create_action(project, args) {
  // 处理create
  try {
    // 先判断需要创建的文件夹是否已经存在
    if (fs.existsSync(project)) {
      console.log(`当前目录已经存在目标文件夹(${project})，请更换项目名`)
      return
    }

    const answer = await inquirer.prompt([
      { type: 'list', name: 'framework', choices: frameworkArr, message: '请选择你需要的框架' }
    ])

    // 根据用户选择的框架，下载对应的代码。
    const spinner = ora('下载中...').start()
    download('direct:' + gitAddress[answer.framework], project, { clone: true }, (err) => {
      if (err) {
        spinner.fail(err)
      } else {
        // spinner.info('一些提示信息')
        spinner.succeed('下载成功')
        console.log(`you can run： `)
        console.log(chalk.red.bold(`  cd ${project}`));
        console.log(chalk.red.bold(`  npm install`));
        console.log(chalk.red.bold(`  npm run dev`));
      }
    })
  } catch (err) {
    console.log(err)
  }
}

module.exports = {
  create_action
}
